<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use Illuminate\Http\Request;
use App\Transformers\UserTransformer;
use App\Http\Requests\Api\UserRequest;
use App\Models\Image;

class UsersController extends Controller{
    public function store(UserRequest $request){
        $verifyData = \Cache::get($request->verification_key);
        if(!$verifyData){
            return $this->response->error('验证码已失效',422);
        }

        if(!hash_equals($verifyData['code'],$request->verification_code)){
            // 返回401
            return $this->response->errorUnauthorized('验证码错误');
        }
        $user = new User();
        $user->name = $request->name;
        $user->phone = $verifyData['phone'];
        $user->password = bcrypt($request->password);
        $user->save();
        // 清除验证码缓存
        \Cache::forget($request->verification_key);
        //return $this->response->created();
        return $this->response->item($user,new UserTransformer())
            ->setMeta([
                'access_token' => \Auth::guard('api')->fromUser($user),
                'token_type' => 'Bearer',
                'expires_in' => \Auth::guard('api')->factory()->getTTL() * 60
            ])
            ->setStatusCode(201);
    }

    // 获取个人用户信息
    public function me(){
        return $this->response->item($this->user(),new UserTransformer());
    }

    // 更新用户信息
    public function update(UserRequest $request){
        $user = $this->user();
        $attributes = $request->only(['name','email','introduction']);
        if($request->avatar_image_id){
            $image = Image::find($request->avatar_image_id);
            $attributes['avatar'] = $image->path;
        }

        $user->update($attributes);
        return $this->response->item($user,new UserTransformer());
    }

    // 获取活跃用户
    public function activedIndex(User $user){
        return $this->response->collection($user->getActiveUsers(),new UserTransformer());
    }
}
